home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_12 / splash / splash.h < prev    next >
Text File  |  1993-01-14  |  23KB  |  856 lines

  1. /*
  2.  * Version 1.8
  3.  * Written by Jim Morris,  jegm@sgi.com
  4.  * Kudos to Larry Wall for inventing Perl
  5.  * Copyrights only exist on the regex stuff, and all have been left intact.
  6.  * The only thing I ask is that you let me know of any nifty fixes or
  7.  * additions.
  8.  * 
  9.  * Credits:
  10.  * I'd like to thank Michael Golan <mg@Princeton.EDU> for his critiques
  11.  * and clever suggestions. Some of which have actually been implemented
  12.  */
  13.  
  14. #ifndef    _SPLASH_H
  15. #define    _SPLASH_H
  16.  
  17. #include <string.h>
  18. #include "regexp.h"
  19.  
  20. #if    DEBUG
  21. #include    <stdio.h>
  22. #endif
  23.  
  24. #define    INLINE    inline
  25.  
  26. //************************************************************
  27. // This is the base class for SPList, it handles the underlying
  28. // dynamic array mechanism
  29. //************************************************************
  30.  
  31. template<class T>
  32. class SPListBase
  33. {
  34. private:
  35.     enum{ALLOCINC=20};
  36.     T *a;
  37.     int cnt;
  38.     int first;
  39.     int allocated;
  40.     int allocinc;
  41.     void grow(int amnt= 0, int newcnt= -1);
  42.  
  43. protected:
  44.     void compact(const int i);
  45.  
  46. public:
  47. #ifdef    USLCOMPILER
  48.     // USL 3.0 bug with enums losing the value
  49.     SPListBase(int n= 20)
  50. #else
  51.     SPListBase(int n= ALLOCINC)
  52. #endif
  53.     {
  54.     a= new T[n];
  55.     cnt= 0;
  56.         first= n>>1;
  57.     allocated= n;
  58.     allocinc= n;
  59. #    ifdef    DEBUG
  60.     fprintf(stderr, "SPListBase(int %d) a= %p\n", allocinc, a);
  61. #    endif
  62.     }
  63.  
  64.     SPListBase(const SPListBase<T>& n);
  65.     SPListBase<T>& SPListBase<T>::operator=(const SPListBase<T>& n);
  66.     virtual ~SPListBase(){
  67. #       ifdef    DEBUG
  68.     fprintf(stderr, "~SPListBase() a= %p, allocinc= %d\n", a, allocinc);
  69. #       endif
  70.     delete [] a;
  71.     }
  72.  
  73.     INLINE T& operator[](const int i);
  74.     INLINE const T& operator[](const int i) const;
  75.  
  76.     int count(void) const{ return cnt; }
  77.  
  78.     void add(const T& n);
  79.     void add(const int i, const T& n);
  80.     void erase(void){ cnt= 0; first= (allocated>>1);}
  81. };
  82.  
  83. // forward declarations
  84. class SPStringList;
  85. class Slice;
  86. template <class T> class SPList;
  87. template <class T> class SubList;
  88.  
  89. //************************************************************
  90. // Slice class to keep track of, and create, slices
  91. //************************************************************
  92.  
  93. #include <stdarg.h>
  94. class Slice
  95. {
  96. private:
  97.     SPList<Range> *rl;
  98.  
  99. public:
  100.     inline Slice();
  101.     Slice(const char *); // parse the string to get a slice
  102.     Slice(int n, ...); // list of indices to add to slice
  103.     inline Slice(const Slice& slc);
  104.     inline Slice(const Range& r);
  105.     inline ~Slice();
  106.      
  107.     inline int count(void) const;
  108.     inline const Range& operator[](int i) const;
  109.     void add(int i); // add one element to slice
  110.     friend ostream& operator<<(ostream&, const Slice&);
  111. };
  112.  
  113. //************************************************************
  114. // Allows assignment to slices of a list
  115. //************************************************************
  116.  
  117. template <class T>
  118. class SubList
  119. {
  120. private:
  121.     // This has to be a pointer because we don't know the size of Splice
  122.     // and there is a nasty cyclic interdependency between the next 3 classes
  123.     Slice *sl; // because we may want to use a temp in call T/O convenience with efficiency
  124.     SPList<T>& l;
  125.  
  126. public:
  127.     SubList(SPList<T>& lst, const Slice& slc);
  128.     SubList(SPList<T>& lst, int st, int len);
  129.     SubList(SPList<T>& lst, const Range& r);
  130.     ~SubList();
  131.  
  132.     SubList<T>& operator=(const SPList<T>& lst);
  133.     friend SPList<T>;
  134. };
  135.  
  136. //************************************************************
  137. // SPList
  138. //************************************************************
  139.  
  140. template <class T>
  141. class SPList: private SPListBase<T>
  142. {
  143. public:
  144.  
  145.     SPList(int sz= 10): SPListBase<T>(sz){}
  146.     SPList(const SubList<T>& sbl);
  147.     
  148.     // stuff I want public to see from SPListBase
  149.     T& operator[](const int i){return SPListBase<T>::operator[](i);}
  150.     const T& operator[](const int i) const{return SPListBase<T>::operator[](i);}
  151.     SPListBase<T>::count;   // some compilers don't like this
  152.  
  153.     // add perl-like synonyms
  154.     void reset(void){ erase(); }
  155.     int scalar(void) const { return count(); }
  156.  
  157.     operator void*() { return count()?this:0; } // so it can be used in tests
  158.     int isempty(void) const{ return !count(); } // for those that don't like the above (hi michael)
  159.  
  160.     T pop(void)
  161.     {
  162.     T tmp;
  163.     int n= count()-1;
  164.     if(n >= 0){
  165.         tmp= (*this)[n];
  166.         compact(n);
  167.     }
  168.     return tmp;
  169.     }
  170.  
  171.     void push(const T& a){ add(a);}
  172.     void push(const SPList<T>& l);
  173.  
  174.     T shift(void)
  175.     {
  176.     T tmp= (*this)[0];
  177.     compact(0);
  178.     return tmp;
  179.     }
  180.     
  181.     int unshift(const T& a)
  182.     {
  183.     add(0, a);
  184.     return count();
  185.     }
  186.     
  187.     int unshift(const SPList<T>& l);
  188.  
  189.     SPList<T> reverse(void);
  190.     SPList<T> sort();
  191.     
  192.     SPList<T> splice(int offset, int len, const SPList<T>& l);
  193.     SPList<T> splice(int offset, int len);
  194.     SPList<T> splice(int offset);
  195.  
  196.     SubList<T> operator()(int st, int len){return SubList<T>(*this, st, len);}
  197.     SubList<T> operator()(const Range& r){return SubList<T>(*this, r);}
  198.     SubList<T> operator()(const Slice& slc){return SubList<T>(*this, slc);}
  199.     SubList<T> operator()(const char *s){return SubList<T>(*this, Slice(s));}
  200. };
  201.  
  202. //****************************************************************
  203. // just a mechanism for self deleteing strings which can be hacked
  204. //****************************************************************
  205.  
  206. class TempString
  207. {
  208. private:
  209.     char *str;
  210. public:
  211.     TempString(const char *s)    
  212.     {
  213.         str= new char[strlen(s) + 1];
  214.         strcpy(str, s);
  215.     }
  216.     
  217.     TempString(const char *s, int len)    
  218.     {
  219.         str= new char[len + 1];
  220.         if(len) strncpy(str, s, len);
  221.         str[len]= '\0';
  222.     }
  223.  
  224.     ~TempString(){ delete [] str; }
  225.  
  226.     operator char*() const { return str; }
  227. };
  228.  
  229. //************************************************************
  230. // This class takes care of the mechanism behind variable
  231. // length strings
  232. //************************************************************
  233.  
  234. class VarString
  235. {
  236. private:
  237.     enum{ALLOCINC=32};
  238.     char *a;
  239.     int len;
  240.     int allocated;
  241.     int allocinc;
  242.     INLINE void grow(int n= 0);
  243.  
  244. public:
  245. #ifdef    USLCOMPILER
  246.     // USL 3.0 bug with enums losing the value
  247.     INLINE VarString(int n= 32);
  248. #else
  249.     INLINE VarString(int n= ALLOCINC);
  250. #endif
  251.  
  252.     INLINE VarString(const VarString& n);
  253.     INLINE VarString(const char *);
  254.     INLINE VarString(const char* s, int n);
  255.     INLINE VarString(char);
  256.  
  257.     ~VarString(){
  258. #       ifdef    DEBUG
  259.     fprintf(stderr, "~VarString() a= %p, allocinc= %d\n", a, allocinc);
  260. #       endif
  261.     delete [] a;
  262.     }
  263.  
  264.     VarString& operator=(const VarString& n);
  265.     VarString& operator=(const char *);
  266.  
  267.     INLINE const char operator[](const int i) const;
  268.     INLINE char& operator[](const int i);
  269.  
  270.     operator const char *() const{ return a; }
  271.  
  272.     int length(void) const{ return len; }
  273.  
  274.     void add(char);
  275.     void add(const char *);
  276.     void add(int, const char *);
  277.     void remove(int, int= 1);
  278.  
  279.     void erase(void){ len= 0; }
  280. };
  281.  
  282. class SPStringList;
  283.  
  284. //************************************************************
  285. // Implements the perl specific string functionality 
  286. //************************************************************
  287.  
  288. class SPString
  289. {
  290. private:
  291.     VarString pstr;  // variable length string mechanism
  292.     
  293. public:
  294.     class substring;
  295.     
  296.     SPString():pstr(){}
  297.     SPString(const SPString& n) : pstr(n.pstr){}     
  298.     SPString(const char *s) : pstr(s){}
  299.     SPString(const char c) : pstr(c){}
  300.     SPString(const substring& sb) : pstr(sb.pt, sb.len){}
  301.     
  302.     SPString& operator=(const char *s){pstr= s; return *this;}        
  303.     SPString& operator=(const SPString& n); 
  304.     SPString& operator=(const substring& sb);
  305.  
  306.     operator const char*() const{return pstr;}
  307.     const char operator[](int n) const{ return pstr[n]; }
  308.  
  309.     int length(void) const{ return pstr.length(); }
  310.     
  311.     char chop(void);
  312.     
  313.     int index(const SPString& s, int offset= 0);    
  314.     int